﻿// JScript 文件

Ext.onReady(function () {
    // 使用表单提示
    Ext.QuickTips.init();
    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';

    var sm = new Ext.grid.CheckboxSelectionModel();

    var cm = new Ext.grid.ColumnModel([
	    new Ext.grid.RowNumberer(),
		sm,
		{ header: '订单号', width: 110, align: 'center', dataIndex: 'orderNO' },
		{ header: '客户名', width: 90, align: 'center', dataIndex: 'cusLoginName' },
		{ header: '金额', width: 100, align: 'center', dataIndex: 'totalMoney' },
        { header: '付款时间', width: 100, align: 'center', dataIndex: 'payTime' },
        { header: '付款方式', width: 90, align: 'center', dataIndex: 'payMethod' }, 
        { header: '快递', width: 150, align: 'center', dataIndex: 'expressID',renderer:function(v){
            if(v==1) { return "顺风快递" ;}
            else if(v==2) { return "申通快递"; }
            else if(v==3) { return "EMS"; }
            else if(v==4) { return "汇通快运"; } 
            else if(v==5) { return "圆通速递"; } 
            else if(v==6) { return "TNT快递"; } 
            else if(v==7) { return "韵达快运"; } 
            else if(v==8) { return "中通快递"; } 
            else if(v==9) { return "宅急送"; } 
            else if(v==10) { return "中铁快运"; } 
            else if(v==11) { return "联邦快递"; } 
            else if(v==12) { return "德邦物流"; }  
            else { return "";}             
            
        } },
        { header: '快递单号', width: 140, align: 'center', dataIndex: 'expressNO' }, 
        { header: '状态', width: 70, align: 'center', dataIndex: 'status', renderer: function (v) {
            if (v == 0) {
                return "未付款";
            } else if (v == 1) {
                return "已付款";
            } else if (v == 2) {
                return "已发货";
            }
        }
        }
	]);

    cm.defaultSortable = true;

    var ds = new Ext.data.GroupingStore({
        proxy: new Ext.data.HttpProxy({
            url: 'ashx/SalesOrder_GetByPage.ashx'
        }),
        reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
        },
		[{ name: 'id', mapping: 'orderID', type: 'string' },
         { name: 'cusLoginName',  type: 'string' },
		 { name: 'totalMoney', type: 'string' },
         { name: 'payTime', type: 'string' },
		 { name: 'payMethod', type: 'string' },
		 { name: 'expressID', type: 'string' },
         { name: 'expressNO', type: 'string' },
         { name: 'status', type: 'string' } ,
         { name: 'orderNO', type: 'string' } 
		])
    });
    var typeData = new Ext.data.SimpleStore( { fields : ["typeValue", "text"], data:[["-1","所有"],["0","未付款"],["1","已付款"],["2","已发货"]] } );
    var startDate =  new Ext.form.DateField({
		        id: 'startDate',
		        name:'startDate',
		        width: 80,
		        fieldLabel : '开始日期：',  
               emptyText : '请选择',
                
               endDateField:"endDate"
		    });
	var endDate = new Ext.form.DateField({
		        id: 'endDate',
		        name:'endDate',
		        width: 80,
		        fieldLabel : '结束日期：',  
               emptyText : '请选择',
               
               startDateField :"startDate"
		    });
    var searchbar = new Ext.Toolbar({
        id: 'searchbar',
        items: ['订单号  ： ',
		    new Ext.form.TextField({
		        id: 'orderNO',
		        width: 110
		    }), ' ',
		    ' 登陆名  ： ',
		    new Ext.form.TextField({
		        id: 'loginname',
		        width: 100
		    }), ' ',
		    ' 状态  ： ',
		    new Ext.form.ComboBox({
		        id: 'status',
		        width: 80,
		        store : typeData,
		        mode : "local",
		        editable : false,
		        valueField : "typeValue",
                displayField : "text",
                hiddenName : "orderStatus", 
                triggerAction: 'all' 		        
		    }), ' ',
//		     ' 开始日期  ： ',
//		    startDate, ' ',
//		    ' 结束日期  ： ',
//		    endDate, ' ',
			new Ext.Toolbar.Button({
			    id: 'searchbutton',
			    iconCls: 'icon_search',
			    text: '查询',
			    handler: function(){ 
//			        var strStartDate = Ext.get('startDate').getValue();
//			        alert(strStartDate);
			        doSearch();
			        
			    }
			})
		]
    });

    function doSearch() {
        ds.load(
	        { params: {
	            start: 0,
	            limit: 20,
	            orderNO: Ext.get('orderNO').dom.value,
	            loginname:Ext.get('loginname').dom.value,
	            status:Ext.get('orderStatus').getValue()
//	            ,
//	            startDate:Ext.get('startDate').getValue(),
//	            endDate: Ext.get('endDate').getValue()
	        }
	        });
    }

    var paging = new Ext.PagingToolbar({
        pageSize: 20,
        store: ds,
        displayInfo: true,
        displayMsg: '显示第{0}条到{1}条记录，一共{2}条',
        items:
		[
//		"-", " ",
//		    { text: '添加',
//		        tooltip: '添加一条系统用户信息',
//		        iconCls: 'icon-add',
//		        handler: function () {
//		            addNewUser();
//		        }
//		    },

		 "-", " ", { text: '发货',
		     tooltip: '订单发货',
		     iconCls: 'icon-edit',
		     handler: function () {

		         var _record = grid.getSelectionModel().getSelected();
		         if (sm.getCount() == 1) {
		             Delivery(_record );
		         } else
		             Ext.Msg.alert('提示', '请选择一条记录！');
		         

		     }
		 },
	    "-", " ",
	    { text: '详细信息',
	        tooltip: '查看详细信息',
	        iconCls: 'icon-edit',
	        handler: function () {
	            var _record = grid.getSelectionModel().getSelected();
	            if (sm.getCount() == 1) {
	                 //window.open("salesOrderInfo.aspx?orderID=" + _record.data.id);
	                ShowDetail(_record.data.id);                 
	            } else
	                Ext.Msg.alert('提示', '请选择一条记录！');
	        }
	    }
		],
        emptyMsg: "查询记录为空!"
    });
    
    var grid = new Ext.grid.EditorGridPanel({
        renderTo: 'grid-div',
        cm: cm,
        ds: ds,
        sm: sm,
        tbar: searchbar,
        bbar: paging,
        height: 500,
        border: true,
        bodyStyle: 'width:1020px',
        stripeRows: true,
        title: '订单信息'
    });

    ds.load({ params: { start: 0, limit: 20} });
    function ShowDetail( id)
    {
        var window_detail = new Ext.Window({
                //applyTo:'hello-win', 
                title:'订单详细信息', 
                layout:'fit', 
                width:680, 
                height:480,
                resizable: false,
                autoHeight: true,
                modal: true,
                closeAction:'hide',
                plain: true, 
                items: [{  title: '弹出的窗口', 
                                    header:false, 
                                    html : '<iframe style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; width: 728px; height: 455px; border-right-width: 0px" src="salesOrderInfo.aspx?orderID=' + id + '" frameborder="0" width="100%" scrolling="auto" height="100%"></iframe>', 
                                    border:false 
                                }]

            }); 
        window_detail.show();   
            
    }
//    function del_user(record,forb) {

//        var myMask = new Ext.LoadMask(Ext.getBody(), {
//            msg: "Please wait..."
//        });
//        Ext.Ajax.request({
//            url: 'ashx/Custom_Del.ashx',
//            params:{
//                 cusId:record.data.id,
//                 forbid:forb
//            },
//            success: function (response) {
//                ds.load();
//                myMask.hide();
//            },
//            failure: function () {

//                Ext.Msg.alert('提示', '操作失败！');
//                myMask.hide();
//            }
//        });
//    }
 function Delivery(record) {
        if(record.data.status!="1" && record.data.status!="2")
        {
            alert("订单还未付款！");
            return;
        }
        var window_delivery = new Ext.Window({
            title: '快递单号',
            width: 340,
            height: 580,
            resizable: false,
            autoHeight: true,
            modal: true,
            closeAction: 'hide',
            listeners: {
                'hide': function () {
                    this.setTitle('快递单号');
                }
            },
            items: [{
                xtype: "form",
                labelWidth: 70,
                labelAlign: 'right',
                url: 'ashx/SalesOrder_SetExpress.ashx',
                border: false,
                baseCls: 'x-plain',
                bodyStyle: 'padding:5px 5px 0',
                anchor: '100%',
                defaults: {
                    width: 317,
                    msgTarget: 'side'
                },
                items: [{
                    xtype: "fieldset",
                    title: '',
                    autoHeight: true,
                    items: [{
                        xtype: 'label',
                        id: 'order_orderNO',
                        name: 'order.orderNO',
                        value: record.data.orderNO
                    },{
                        xtype: 'hidden',
                        id: 'order_orderId',
                        name: 'order.orderId',
                        value: record.data.id
                    }, {
                        xtype: "textfield",
                        width: 200,
                        fieldLabel: "快递号",
                        name: "order.expressNO",
                        value: record.data.expressNO,
                        regex: /^\S+$/,
                        regexText: "请输入快递号!",
                        allowBlank: false,
                        blankText: '该字段不允许为空',
                        msgTarget: 'side',
                        maxLength: 50
                    } , {
                        width: 200,
                        anchor: "94%",
                        xtype: "combo",
                        fieldLabel: "快递公司",
                        name: "order.expressName", 
                        store: new Ext.data.SimpleStore({
                            fields: ["value", "text"],
                            data: [ ['1', '顺风快递'], 
                                    ['2', '申通快递'], 
                                    ['3', 'EMS'], 
                                    ['4', '汇通快运'], 
                                    ['5', '圆通速递'], 
                                    ['6', 'TNT快递'], 
                                    ['7', '韵达快运'], 
                                    ['8', '中通快递'], 
                                    ['9', '宅急送'], 
                                    ['10', '中铁快运'], 
                                    ['11', '联邦快递'], 
                                    ['12', '德邦物流'] 
                                        
                            ]
                        }),
                        displayField: 'text',
                        valueField: 'value',
                        //value: record.data.expressID,
                        mode: 'local',
                        emptyText: '请选择快递公司',
                        allowBlank: false,
                        blankText: '该字段不允许为空',
                        msgTarget: 'side',
                        editable: false,
                        triggerAction: 'all',
                        listWidth: 200,
                        listeners: {
                            select: function (combo, record, index) {
                                //alert(this.getValue());
                                this.ownerCt.ownerCt.form.findField('order.expressName').setValue(this.getValue());
                            }
                        }

                    }]
                }],
                buttonAlign: 'center',
                minButtonWidth: 60,
                buttons: [{
                    text: '确定',
                    handler: function (btn) {
                        var frm = this.ownerCt.ownerCt.form;
                        if (frm.isValid()) {
                            btn.disable();
                            var cnfield = frm.findField('order.orderId');
                            frm.submit({
                                waitTitle: '请稍候',
                                waitMsg: '正在提交表单数据,请稍候...',
                                success: function (form, action) {

                                    Ext.Msg.show({
                                        title: '成功提示',
                                        msg:  '修改成功!',
                                        buttons: Ext.Msg.OK,
                                        fn: function () {
                                            cnfield.focus(true);
                                            btn.enable();
                                        },
                                        icon: Ext.Msg.INFO
                                    });
                                    cnfield.reset();
                                    window_delivery.hide();
                                    ds.load();
                                },
                                failure: function () {
                                    Ext.Msg.show({
                                        title: '错误提示',
                                        msg: '添加失败，请联系管理员!',
                                        buttons: Ext.Msg.OK,
                                        fn: function () {
                                            btn.enable();
                                        },
                                        icon: Ext.Msg.ERROR
                                    });
                                }
                            });
                        }
                    }
                }, {
                    text: '取消',
                    handler: function () {
                        window_delivery.hide();
                    }
                }]
            }]
        });

        window_delivery.show();
        

    }


    


});
 